package com.scirillo.frases.ui;

import com.actionbarsherlock.app.SherlockFragment;
import com.scirillo.frases.R;
import com.scirillo.frases.util.Util;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;


@SuppressLint("ValidFragment")
public class FrasesFragment extends SherlockFragment {
	private Button mDummyButton;
	private TextView mDummyContent;
	private String[] frases;
	private int index;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		 return (FrameLayout)inflater.inflate(R.layout.frase, container, false);
	}
	
	
	public FrasesFragment() { 
		this(0);
	}
	


	public FrasesFragment(int i) {
		index = i;
		setRetainInstance(true);	
	}
	
	@Override
	public void onStart() {
		mDummyContent = (TextView) getView().findViewById(R.id.fullscreen_content);	
		mDummyContent.setMovementMethod(new ScrollingMovementMethod());
		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/MiniverRegular.ttf");
		 mDummyContent.setTypeface(tf);
		frases = getResources().getStringArray(R.array.frases);		
		
		registerForContextMenu(mDummyContent);
		String text = Util.removeExtra(frases[index]);
		mDummyContent.setMovementMethod(LinkMovementMethod.getInstance());
		mDummyContent.setText(text);		
		super.onStart();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    //user has long pressed your TextView
	    menu.add(0, v.getId(), 0, "Copy");
	    TextView mDummyContent = (TextView) v;
	    putText(mDummyContent.getText().toString());
	}
	
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public void putText(String text){
	    int sdk = android.os.Build.VERSION.SDK_INT;
	    if(sdk < android.os.Build.VERSION_CODES. HONEYCOMB) {
	        android.text.ClipboardManager clipboard = (android.text.ClipboardManager)  getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
	        clipboard.setText(text);
	    } else {
	        android.content.ClipboardManager clipboard = (android.content.ClipboardManager)  getActivity().getSystemService(Context.CLIPBOARD_SERVICE); 
	        android.content.ClipData clip = ClipData.newPlainText("simple text",text);
	        clipboard.setPrimaryClip(clip);
	    }
	}

}
